package cn.kgm.producer;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;

import java.util.Arrays;
import java.util.List;

/**
 * 单向发送消息
 * Create by kappi on
 * 2021/8/24/024 13:44
 */
public class OnewayProducer {
    private static final List<String> userNames = Arrays.asList("张三", "李嘞", "知晓", "迪诺", "李四" );

    public static void main(String[] args) throws Exception {
        //Instantiate with a producer group name.
        DefaultMQProducer producer = new DefaultMQProducer("kagim" );
        // Specify name server addresses.
        producer.setNamesrvAddr("localhost:9876" );
        //Launch the instance.
        producer.start();
        for (int i = 0; i < 5; i++) {
            //Create a message instance, specifying topic, tag and message body.
            Message msg = new Message("kagimTopic" /* Topic */,
                    "TagA" /* Tag */,
                    (userNames.toString()).getBytes(RemotingHelper.DEFAULT_CHARSET) /* Message body */
            );
            //Call send message to deliver message to one of brokers.
            producer.sendOneway(msg);
        }
        //Wait for sending to complete
        Thread.sleep(5000);
        producer.shutdown();
    }
}
